<template>
  <div class="login">
    <el-row justify="center" align="middle" style="height: 100vh; width: 100%;">
    <el-col :span="8" style="width: 100%;">
      <el-card style="border-radius: 12px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);">
        <h2 style="text-align:center; font-weight:600; margin-bottom:24px; color:#fff">课程系统登录</h2>
        <el-form :model="form" :rules="rules" ref="loginForm" label-width="80px">
          <el-form-item prop="username">
            <el-input v-model="form.username" autocomplete="off" size="large" placeholder="用户名">
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="form.password" type="password" autocomplete="off" size="large" placeholder="密码">
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onLogin" :loading="loading" size="large" style="width:100%; margin-bottom: 12px;">登录</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="text" @click="goRegister" size="large" style="width:100%;">没有账号？去注册</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-col>
  </el-row>
  </div>
  
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
// import axios from 'axios'
import { ElMessage } from 'element-plus'
import axios from '../axios'
import { User, Lock } from '@element-plus/icons-vue'
const router = useRouter()
const form = ref({ username: '', password: '' })
const loading = ref(false)
const loginForm = ref(null)
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const onLogin = () => {
  loginForm.value.validate(async valid => {
    if (!valid) return
    loading.value = true
    try {
      const res = await axios.post('/api/user/login', {
        username: form.value.username,
        password: form.value.password
      })
      if (res.data && typeof res.data === 'string' && res.data.length === 36) {
        // 假设userId为36位字符串
        localStorage.setItem('userId', res.data)
        localStorage.setItem('username', form.value.username)
        ElMessage.success('登录成功')
        router.push('/home')
      } else {
        ElMessage.error(res.data || '登录失败')
      }
    } catch (e) {
      ElMessage.error('网络错误')
    } finally {
      loading.value = false
    }
  })
}

const goRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login {
  background: url(../assets/login.jpg);
}
.el-card {
  margin: auto;
  background: rgb(0,0,0,0.5);
  border: 0;
}

:deep(.el-form-item__content) {
  margin-left: 0!important;
}
:deep(.el-form) {
  width: 80%;
  margin: auto;
}
:deep(body) {
  background: linear-gradient(135deg, #6a82fb 0%, #b3c6ff 50%, #e0c3fc 100%) !important;
  min-height: 100vh !important;
}
</style>